//
//  NSString+BLExe.m
//  BLNew
//
//  Created by ss on 2019/7/13.
//  Copyright © 2019 chehang168. All rights reserved.
//

#import "NSString+BLExe.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (BLExe)

+ (BOOL)blIsBlankString:(NSString *)string{
    if (![string isKindOfClass:[NSString class]]) {
        return YES;
    }
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"] || [string isEqualToString:@"<null>"]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    }
    return NO;
}

#pragma mark ---- 查找某个字符串在整体字符串中出现的位置集合
+(NSMutableArray *)blCaluteSubStringLocationCount:(NSString *)content subStr:(NSString *)selectStr{
    int location = 0;
    NSMutableArray *locationArr = [[NSMutableArray alloc] initWithCapacity:1];
    NSRange range = [content rangeOfString:selectStr];
    if (range.location != NSNotFound) {
       // 查到了
        // 声明临时变量。记录截取后的字符串
        NSString *subStr = content;
        while (range.location != NSNotFound) {
            if (location == 0) {
                location += range.location;
            }else{
                location += range.location + selectStr.length;
            }
            // 记录位置
            NSNumber *number = [NSNumber numberWithUnsignedInteger:location];
            [locationArr addObject:number];
            // 每次记录之后，把找到的字符串截取掉
            subStr = [subStr substringFromIndex:(range.location + range.length)];
            range = [subStr rangeOfString:selectStr];
        }
        
        return locationArr;
    }else{
        // 查不到，返回空
        return nil;
    }
}

- (CGFloat) heightWithSize:(CGFloat) fs width:(CGFloat) width{
    UIFont *font = [UIFont systemFontOfSize:fs];
    CGSize size = CGSizeMake(width,MAXFLOAT);
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    size = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
    return size.height + 2.0f;
}

- (CGFloat) getHeightWithFont:(UIFont*)font width:(CGFloat)width{
    CGSize size = CGSizeMake(width,MAXFLOAT);
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    size = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading attributes:tdic context:nil].size;
    return size.height + 2.0f;
}

- (CGFloat) getStringWidthFont:(UIFont*)font height:(CGFloat)height{
    NSDictionary *dic = @{NSFontAttributeName:font};
    CGRect rect = [self boundingRectWithSize:CGSizeMake(MAXFLOAT,height)  options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width + 2.0f;
}

- (CGFloat) heightAttributesWithSize:(CGFloat) fs width:(CGFloat) width {
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:fs]};
    CGRect rect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                  attributes:attributes
                                     context:nil];
    return rect.size.height + 2.0f;
}
 
+ (CGSize)bl_SizeLabel:(NSString *)title font:(UIFont*)font width:(CGFloat)width line:(NSInteger)line{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.font = font;
    label.numberOfLines = line;
    label.text = title;
    [label sizeToFit];
    return label.frame.size;
}

+(CGFloat)getLabelHeight:(NSString *)title font:(UIFont*)font width:(CGFloat)width line:(NSInteger)line{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.font = font;
    label.numberOfLines = line;
    label.text = title;
    [label sizeToFit];
    return label.frame.size.height;
}

+(CGFloat)getButtonWidth:(NSString *)title font:(NSInteger)integer{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = [UIFont systemFontOfSize:integer];
    [button setTitle:title forState:UIControlStateNormal];
    [button sizeToFit];
    return button.frame.size.width;
}

- (NSString *)blmd5Encrypt{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (uint32_t)strlen(cStr), result);
    return [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+ (NSDictionary *)bl_DictionaryWithJsonString:(NSString *)jsonString{
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

+ (NSString *)bldDayString {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:[NSDate date]];
}



+ (NSString *)dateToString:(NSString *)date {
   // 初始化时间格式控制器
   NSDateFormatter *matter = [[NSDateFormatter alloc] init];
   // 设置设计格式
   [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
   // 进行转换
   NSTimeInterval time = [date doubleValue] + 28800;
   NSDate * Date = [NSDate dateWithTimeIntervalSince1970:time];
   NSString *dateStr = [matter stringFromDate:Date];
   return dateStr;
}
+ (NSString *)dateToString:(NSString *)date Format:(NSString *)format{
   // 初始化时间格式控制器
   NSDateFormatter *matter = [[NSDateFormatter alloc] init];
   // 设置设计格式
   [matter setDateFormat:format];
   // 进行转换
   NSTimeInterval time = [date doubleValue];
   NSDate * Date = [NSDate dateWithTimeIntervalSince1970:time];
   NSString *dateStr = [matter stringFromDate:Date];
   return dateStr;
}

+ (NSString *)stringToDate:(NSString *)dateStr {
   
   // 初始化时间格式控制器
   NSDateFormatter *matter = [[NSDateFormatter alloc] init];
   // 设置设计格式
   [matter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzz"];
   NSTimeInterval time = [dateStr doubleValue];
   NSDate * Date = [NSDate dateWithTimeIntervalSince1970:time];
   // 进行转换
   NSString *date = [matter stringFromDate:Date];
   
   return date;
}
+ (NSString *)stringToDate:(NSString *)dateStr Format:(NSString *)format{
   
   // 初始化时间格式控制器
   NSDateFormatter *matter = [[NSDateFormatter alloc] init];
   // 设置设计格式
   [matter setDateFormat:format];
   NSTimeInterval time = [dateStr doubleValue];
   NSDate * Date = [NSDate dateWithTimeIntervalSince1970:time];
   // 进行转换
   NSString *date = [matter stringFromDate:Date];
   return date;
}

@end
